<template>
  <div class="container">
    <div class="nav">
      <div class="left">
        <span class="option">实时监控</span>
      </div>
    </div>
    <div class="body">
      <div class="videos">
        <component :is="getComponentType(rtsps[item].dataFlow)" v-for="item in rtspArr" :key="item" :url="rtsps[item].dataFlow" />
      </div>
      <div class="options">
        <div class="option" v-for="(item, idx) in rtsps" @click="clickOption(idx)" :key="item.name">
          <span class="desc" :class="{ close: item.open == false, open: item.open == true }">◉&nbsp;&nbsp;</span>
          <span class="desc">{{ item.name }}</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { onMounted, reactive, ref, watch } from 'vue';
import Rtsp from '@/components/Rtsp/index.vue'
import HLS from '@/components/HLS/index.vue'
import { ElMessage } from 'element-plus';
import { reqOperationDevice } from '@/api/device'
import useOperationStore from '../../store/operation';
let rtsps = reactive([])
let rtspArr = reactive([])
// 组件选择器
const getComponentType=(url)=>{
  if(url.startsWith("rtsp")){
    return Rtsp
  }
  if(url.endsWith(".m3u8")){
    return HLS
  }
  else return HLS
}
let operationStore=useOperationStore()
let operationId = ref(operationStore.data.length>0?operationStore.data[operationStore.index].id:'')
operationStore.$subscribe(async(mutation, state)=>{
  operationId.value=operationStore.data[operationStore.index].id
  await initData()
})
// 加入设备
const clickOption = (idx) => {
  if (rtsps[idx].open == false) {
    if (rtspArr.length < 4) {
      rtspArr.push(idx);
      rtsps[idx].open = true;
    }
    else {
      ElMessage({
        type: "warning",
        message: "设备位已满"
      })
    }
  }
  else {
    rtspArr.splice(rtspArr.indexOf(idx), 1);
    rtsps[idx].open = false
  }
}


// 摄像头数据拉取
const initData = async () => {
  let res = await reqOperationDevice(operationId.value)
  switch (res.code) {
    case 0:
      rtspArr.splice(0, rtspArr.length)
      rtsps.splice(0, rtsps.length)
      rtsps.push(...(res.data.map((item) => {
        return {
          ...item,
          open: false
        }
      })))
      break;
    case 1:
      setTimeout(() => { $router.push('/login') }, 1000)
      break;
  }
}

onMounted(async () => {
  if (operationId.value) {
    await initData()
  }
})
</script>

<style lang="scss" scoped>
.close {
  color: red;
}

.open {
  color: greenyellow;
}

.container {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 100%;
  height: 100%;

  .nav {
    width: 96%;
    background-color: white;
    height: 6%;
    display: flex;
    align-items: center;
    justify-content: space-between;

    .left {
      display: flex;

      .option {
        border-left: 3px solid rgb(9, 184, 199);
        padding: 5px 10px;
        display: flex;
        align-items: center;
      }
    }
  }

  .body {
    height: 92%;
    width: 96%;
    display: flex;

    .videos {
      width: 90%;
      height: 100%;
      background-color: rgb(76, 65, 65);
      display: grid;
      grid-template-columns: repeat(2, 50%);
      grid-template-rows: repeat(2, 50%);
    }

    .options {
      width: 10%;
      height: 100%;
      background-color: rgb(75, 86, 110);
      display: flex;
      flex-direction: column;
      overflow-y: auto;

      .option {
        padding: 12px 10px;
        font-size: 18px;
        display: flex;
        align-items: center;
        color: white;
      }

      .option:hover {
        background-color: rgb(56, 63, 82);
      }
    }
  }

}
</style>